package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.ruoyi.app.domain.AppImSystemMessage;
import com.ruoyi.app.domain.AppUser;
import com.ruoyi.app.domain.bo.AppImSystemMessageBo;
import com.ruoyi.app.domain.vo.AppImSystemMessageVo;
import com.ruoyi.app.domain.vo.AppUserVo;
import com.ruoyi.app.mapper.AppImSystemMessageMapper;
import com.ruoyi.app.mapper.AppUserMapper;
import com.ruoyi.app.service.IAppImSystemMessageService;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 系统消息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-11
 */
@RequiredArgsConstructor
@Service
public class AppImSystemMessageServiceImpl implements IAppImSystemMessageService {

    private final AppImSystemMessageMapper baseMapper;
    private final AppUserMapper appUserMapper;

    /**
     * 查询系统消息
     */
    @Override
    public AppImSystemMessageVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询系统消息列表
     */
    @Override
    public TableDataInfo<AppImSystemMessageVo> queryPageList(AppImSystemMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppImSystemMessage> lqw = buildQueryWrapper(bo);
        Page<AppImSystemMessageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询系统消息列表
     */
    @Override
    public List<AppImSystemMessageVo> queryList(AppImSystemMessageBo bo) {
        LambdaQueryWrapper<AppImSystemMessage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppImSystemMessage> buildQueryWrapper(AppImSystemMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppImSystemMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getMsgType() != null, AppImSystemMessage::getMsgType, bo.getMsgType());
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), AppImSystemMessage::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getContent()), AppImSystemMessage::getContent, bo.getContent());
        lqw.eq(StringUtils.isNotBlank(bo.getTargetUserId()), AppImSystemMessage::getTargetUserId, bo.getTargetUserId());
        lqw.eq(bo.getIsRead() != null, AppImSystemMessage::getIsRead, bo.getIsRead());
        lqw.eq(bo.getStatus() != null, AppImSystemMessage::getStatus, bo.getStatus());
        lqw.eq(bo.getLookSum() != null, AppImSystemMessage::getLookSum, bo.getLookSum());
        return lqw;
    }

    /**
     * 新增系统消息
     */
    @Override
    public Boolean insertByBo(AppImSystemMessageBo bo) {
        AppImSystemMessage add = BeanUtil.toBean(bo, AppImSystemMessage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改系统消息
     */
    @Override
    public Boolean updateByBo(AppImSystemMessageBo bo) {
        AppImSystemMessage update = BeanUtil.toBean(bo, AppImSystemMessage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppImSystemMessage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除系统消息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<AppUserVo> listUser(AppImSystemMessageBo bo) {
        //JSON转化为List [1,2,3]
        List<Long> userIds = JSONUtil.toList(bo.getTargetUserId(), Long.class);
        LambdaQueryWrapper<AppUser> appUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appUserLambdaQueryWrapper.in(AppUser::getId, userIds);
        List<AppUserVo> appUserVos = appUserMapper.selectVoList(appUserLambdaQueryWrapper);
        return appUserVos;
    }
}
